package com.travel.photos.ui.find;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.ybq.android.spinkit.style.DoubleBounce;
import com.travel.R;
import com.travel.photos.bean.FindDataListBean;
import com.travel.photos.bean.RecommendDataListBean;
import com.travel.photos.image.ImageHepler;
import com.travel.photos.ui.otheruser.OtherUserActivity;
import com.travel.photos.ui.recommend.RecommendFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import tools.HorizontalListView;

/**
 * Created by meidou on 2016/10/18.
 */
public class FindAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;//上下文
    private static final int TYPE_ONE = 0;//searchview
    private static final int TYPE_TWO = 1; //recommendview
    private static final int TYPE_THREE = 2;//recycleview;
    private static final int TYPE_FOUR = 3;//下拉加载更多;
    private  int i;
    private List<FindDataListBean> findlist = new ArrayList<>();
    private List<RecommendDataListBean> recommendlist = new ArrayList<>();
    private GirlOnClickListener mOnClickListener;
    public FindAdapter(Context context,FindContract.View fragment, List<FindDataListBean> findlist, List<RecommendDataListBean> recommendlist) {
        this.context = context;
        this.findlist = findlist;
        mOnClickListener = (GirlOnClickListener) fragment;
        this.recommendlist = recommendlist;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //进行判断显示类型，来创建返回不同的View
        if(viewType==TYPE_ONE){
            SearchViewHolder holder=new SearchViewHolder(LayoutInflater.from(context)
                            .inflate(R.layout.photo_vp_find_re,parent,false));
            return holder;
        }else if(viewType==TYPE_TWO) {
            RecommendViewHolder holder = new RecommendViewHolder(
                    LayoutInflater.from(context)
                            .inflate(R.layout.photo_vp_find_cy,parent,false));
            return holder;
        }else if (viewType==TYPE_THREE)   {
            MyViewHolder holder = new MyViewHolder(
                    LayoutInflater.from(context)
                            .inflate(R.layout.item_photo_find,parent,false));
            return holder;
        }else if(viewType==TYPE_FOUR){
            FootViewHolder footholder=new FootViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.photo_recycler_load_more,parent,false));
            return footholder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof SearchViewHolder){//search
            ((SearchViewHolder)holder).mPhotoFindSerach.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent= new Intent(context,PhotoSearchActivity.class);
                    context.startActivity(intent);
                }
            });
        }else if(holder instanceof RecommendViewHolder){//recommend
            if(recommendlist!=null){
                RecommendAdapter adapter = new RecommendAdapter(context,recommendlist);
                ( (RecommendViewHolder)holder).mPhotoFindListview.setAdapter(adapter);
                ( (RecommendViewHolder)holder).mPhotoFindListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> view, View view1, int i, long l) {
                        Intent intent = new Intent(context, OtherUserActivity.class);
                        intent.putExtra("userId",recommendlist.get(i).getMemberId());
                        intent.putExtra("name",recommendlist.get(i).getNickName());
                        intent.putExtra("image",recommendlist.get(i).getAvatar());
                        context.startActivity(intent);
                    }
                });

                ( (RecommendViewHolder)holder).mPhototMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, RecommendFragment.class);
                        context.startActivity(intent);
                    }
                });

            }
        }else if(holder instanceof FootViewHolder) {//foot
                    ((FootViewHolder) holder).tv_jiazai.setText("上拉加载更多...");
                    /*DoubleBounce doubleBounce = new DoubleBounce();
                    ((FootViewHolder) holder).iv_jiazai.setIndeterminateDrawable(doubleBounce);*/
        }else if(holder instanceof MyViewHolder){
            i = position-2;
            //头像的加载
            ImageHepler.showImage(context,findlist.get(i).getPi().getAuthorImg(),(((MyViewHolder) holder).mPhotoFindItemAuthorImg),true);
            ((MyViewHolder) holder).item_photo_find_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context,OtherUserActivity.class);
                    intent.putExtra("userId",findlist.get(i).getPi().getMemId());
                    intent.putExtra("name",findlist.get(i).getPi().getAuthorName());
                    intent.putExtra("image",findlist.get(i).getPi().getAuthorImg());
                    context.startActivity(intent);
                }
            });
            //大图片的加载
            ImageHepler.showImage(context,findlist.get(i).getPi().getListPicWithSize().getPic(),(((MyViewHolder) holder).mPhotoFindItemPic),false);
            setOnClickListener(((MyViewHolder) holder),findlist.get(i).getPi().getListPicWithSize().getPic());
            GoodAdapter adapter = new GoodAdapter(context,findlist.get(i).getCh());
            ((MyViewHolder) holder).mPhotoFindItemGrid.setAdapter(adapter);
            ((MyViewHolder) holder).mPhotoFindItemAuthorName.setText(findlist.get(i).getPi().getAuthorName());
            ((MyViewHolder) holder).mPhotoFindItemChanCount.setText(findlist.get(i).getPi().getChanCount());
            ((MyViewHolder) holder).mPhotoFindItemCreateTime.setText(findlist.get(i).getPi().getCreateTime());
            if(findlist.get(i).getPi().getDesc()!=null){
                ((MyViewHolder) holder).mPhotoFindItemDesc.setText(findlist.get(i).getPi().getDesc());
                ((MyViewHolder) holder).mPhotoFindItemDesc.setVisibility(View.VISIBLE);

            }
            if(findlist.get(i).getPi().getTopicName()!=""){
                ((MyViewHolder) holder).mPhotoFindItemTopicName.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).text.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).mPhotoFindItemTopicName.setText(findlist.get(i).getPi().getTopicName());
            }
            String Country=findlist.get(i).getPi().getCityCnName() == null?null:findlist.get(i).getPi().getCityCnName();
            String CnName=findlist.get(i).getPi().getCountryCnName() == null?null:"-"+findlist.get(i).getPi().getCountryCnName();
            String PoiCnName=findlist.get(i).getPi().getPoiCnName() == null?null:"-"+findlist.get(i).getPi().getPoiCnName();
            ((MyViewHolder) holder).mPhotoFindItemCountryCnNameCityCnNamePoiCnName.setText(Country+CnName+PoiCnName);
            if(findlist.get(i).getCh().size()>10){
                ((MyViewHolder) holder).tihuanimage.setVisibility(View.VISIBLE);
            }else {
                ((MyViewHolder) holder).tihuanimage.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public int getItemCount() {
        return findlist.size()+3;
    }

    //推荐达人的viewholder
    class RecommendViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.photot_more)
        TextView mPhototMore;
        @Bind(R.id.horizontalist)
        HorizontalListView mPhotoFindListview;

        public RecommendViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    //搜索框的viewholder
    class SearchViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.photo_find_serach)
        LinearLayout mPhotoFindSerach;

        public SearchViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
    //recycle的viewholder
    class MyViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.photo_find_item_authorImg)
        ImageView mPhotoFindItemAuthorImg;
        @Bind(R.id.photo_find_item_authorName)
        TextView mPhotoFindItemAuthorName;
        @Bind(R.id.photo_find_item_createTime)
        TextView mPhotoFindItemCreateTime;
        @Bind(R.id.db)
        TextView mDb;
        @Bind(R.id.photo_find_text)
        TextView text;
        @Bind(R.id.photo_find_item_topicName)
        TextView mPhotoFindItemTopicName;
        @Bind(R.id.photo_find_item_desc)
        TextView mPhotoFindItemDesc;
        @Bind(R.id.photo_find_item_pic)
        ImageView mPhotoFindItemPic;
        @Bind(R.id.tihuanimage)
        ImageView tihuanimage;
        @Bind(R.id.photo_find_item_countryCnName_cityCnName_poiCnName)
        TextView mPhotoFindItemCountryCnNameCityCnNamePoiCnName;
        @Bind(R.id.photo_find_item_chanCount)
        TextView mPhotoFindItemChanCount;
        @Bind(R.id.photo_find_item_grid)
        GridView mPhotoFindItemGrid;
        @Bind(R.id.item_photo_find_layout)
        RelativeLayout item_photo_find_layout;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
    //下拉加载更多布局
    class FootViewHolder extends RecyclerView.ViewHolder{
        TextView tv_jiazai;
        ProgressBar iv_jiazai;
        public FootViewHolder(View itemView) {
            super(itemView);
            tv_jiazai= (TextView)itemView.findViewById(R.id.tv_jiazai);
            iv_jiazai= (ProgressBar)itemView.findViewById(R.id.iv_jiazai);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_ONE;//search布局
        }
        if (position == 1) {
            return TYPE_TWO;//推荐达人的布局
        }
        if (position + 1 == getItemCount()) {
            return TYPE_FOUR;//foot布局
        }
        else {
            return TYPE_THREE;
        }
    }
    //点击事件
    public void setOnClickListener(MyViewHolder holder, final String s) {
        if(s!=null){
            holder.mPhotoFindItemPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"haha",Toast.LENGTH_SHORT).show();
                    mOnClickListener.viewGirlPhoto(s);
                }
            });
        }


    }
}
